﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class ZhiWeiFaBuDengJiServices : IZhiWeiFaBuDengJiServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public ZhiWeiFaBuDengJiServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> Add(ZhiWeiFaBuDengJi zhiWeiFaBuDengJi)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr)) 
                {
                    return con.Execute($"insert into dbo.engage_major_release(first_kind_id, first_kind_name, second_kind_id, second_kind_name, third_kind_id, third_kind_name, major_kind_id, major_kind_name, major_id, major_name, human_amount, engage_type, deadline, register, regist_time, major_describe, engage_required)values('{zhiWeiFaBuDengJi.First_kind_id}','{zhiWeiFaBuDengJi.First_kind_name}','{zhiWeiFaBuDengJi.Second_kind_id}','{zhiWeiFaBuDengJi.Second_kind_name}','{zhiWeiFaBuDengJi.Third_kind_id}','{zhiWeiFaBuDengJi.Third_kind_name}','{zhiWeiFaBuDengJi.Major_kind_id}','{zhiWeiFaBuDengJi.Major_kind_name}','{zhiWeiFaBuDengJi.Major_id}','{zhiWeiFaBuDengJi.Major_name}','{zhiWeiFaBuDengJi.Human_amount}','{zhiWeiFaBuDengJi.Engage_type}','{zhiWeiFaBuDengJi.Deadline}','{zhiWeiFaBuDengJi.Register}','{zhiWeiFaBuDengJi.Regist_time}','{zhiWeiFaBuDengJi.Major_describe}','{zhiWeiFaBuDengJi.Engage_required}')");
                }
            });
        }

        public Task<int> Delete(int mre_id)
        {
            return Task.Run(() => 
            {
                using (SqlConnection con=new SqlConnection(conStr)) 
                {
                    return con.Execute($"delete from dbo.engage_major_release where mre_id='{mre_id}'");
                }
            });
        }

        public Task<FenYe<ZhiWeiFaBuDengJi>> FenYe(int size, int currentPage)
        {
            return Task.Run(() =>
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    DynamicParameters dynamicParameters = new DynamicParameters();
                    dynamicParameters.Add("@pageSize", size);
                    dynamicParameters.Add("@keyName", "mre_id");
                    dynamicParameters.Add("@tabelName", "engage_major_release");
                    dynamicParameters.Add("@where", "");
                    dynamicParameters.Add("@currentPage", currentPage);
                    dynamicParameters.Add("@lie", "*");
                    dynamicParameters.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    string sql = "exec proc_Fenye @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out";
                    List<ZhiWeiFaBuDengJi> list = conn.Query<ZhiWeiFaBuDengJi>(sql, dynamicParameters).ToList();
                    int rows = dynamicParameters.Get<int>("@rows");
                    FenYe<ZhiWeiFaBuDengJi> fenYe = new FenYe<ZhiWeiFaBuDengJi>()
                    {
                        Rows = rows,
                        ZhiWeiFaBuDengJis = list
                    };
                    return fenYe;
                }
            });
        }

        public Task<List<ZhiWeiFaBuDengJi>> FindAll()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<ZhiWeiFaBuDengJi>($"select* from dbo.engage_major_release").ToList();
                }
            });
        }

        public Task<int> Update(ZhiWeiFaBuDengJi zhiWeiFaBuDengJi)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update dbo.engage_major_release set first_kind_name='{zhiWeiFaBuDengJi.First_kind_name}',second_kind_name='{zhiWeiFaBuDengJi.Second_kind_name}', third_kind_name='{zhiWeiFaBuDengJi.Third_kind_name}', major_kind_name='{zhiWeiFaBuDengJi.Major_kind_name}', major_name='{zhiWeiFaBuDengJi.Major_name}', human_amount='{zhiWeiFaBuDengJi.Human_amount}', engage_type='{zhiWeiFaBuDengJi.Engage_type}', deadline='{zhiWeiFaBuDengJi.Deadline}', changer='{zhiWeiFaBuDengJi.Changer}', change_time='{zhiWeiFaBuDengJi.Change_time}', major_describe='{zhiWeiFaBuDengJi.Major_describe}', engage_required='{zhiWeiFaBuDengJi.Engage_required}' where mre_id='{zhiWeiFaBuDengJi.Mre_id}'");
                }
            });
        }

        public Task<List<ZhiWeiFenLei>> ZhiWeiFenLei()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con=new SqlConnection(conStr)) 
                {
                    return con.Query<ZhiWeiFenLei>($"select major_kind_id,major_kind_name from dbo.config_major_kind").ToList();
                }
            });
        }

        public Task<List<ZhiWei>> ZhiWeiName()
        {
            return Task.Run(() => 
            {
                using (SqlConnection con=new SqlConnection(conStr)) 
                {
                    return con.Query<ZhiWei>($"select mak_id,major_id,major_name from dbo.config_major").ToList();
                }    
            });
        }
    }
}
